

$(function(){

    $("#table1").bootstrapTable({
        url: '',
        striped:false,
        columns: [
            {
                field: 'house.id',
                title: 'id',
                visible : false,
            },{
                field: 'costitem.name',
                title: '缴费名称'
            },{
                field: 'costitem.money',
                title: '金额',
                visible : false,
            }
            ,{
                field: 'house.sale.name',
                title: '所属楼宇'
            },{
                field: 'house.danyuan.name',
                title: '所属单元'
            },{
                field: 'house.userdetails.name',
                title: '业主姓名'
            },{
                field: 'house.doornumber',
                title: '门牌号'
            },{
                field: 'lasttime',
                title: '上次读数'
            },{
                field: 'lasttime',
                title: '本次读数',
                formatter: function (value, row, index) {
                    return "<input   type=\"text\" name='ddd' onBlur='diao(this,"+row.costitem.money+")' id='"+row.id+"' value="+row.lasttime+"   style=\"border:0px;outline:medium;\">";
                },
            },{
                field: 'xx',
                title: '本次用量',
                formatter: function (value, row, index) {
                    return "<input   type=\"text\"  name='ddd' id='"+row.id+"y' value='0'   style=\"border:0px;outline:medium;\">";
                },
            },{
                field: 'zmoney',
                title: '应缴费用',
                formatter: function (value, row, index) {
                    return "<input   type=\"text\" id='"+row.id+"j' value='0'   style=\"border:0px;outline:medium;\">";
                },
            },{
                field: 'sdate',
                title: '上次日期'
            },{
                field: 'bdate',
                title: '本次日期',
                formatter: function (value, row, index) {

                    function p(s) {
                        return s < 10 ? '0' + s: s;
                    }

                    var myDate = new Date();

                    var year=myDate.getFullYear();

                    var month=myDate.getMonth()+1;

                    var date=myDate.getDate();
                    var now=year+'-'+p(month)+"-"+p(date);
                    return "<span id='"+row.id+"b'>"+now+"</span>";
                },
            },{
                field: 'jdate',
                title: '缴费期限',
                formatter: function (value, row, index) {

                    function p(s) {
                        return s < 10 ? '0' + s: s;
                    }

                    var myDate = new Date();

                    var year=myDate.getFullYear();

                    var month=myDate.getMonth()+1;

                    var date=myDate.getDate()+7;

                    switch (month) {
                        case ("2"):

                            if(date>28){
                                month+1;
                                date=28-date;
                            }

                            break;
                        case ("1,3,5,7,8,10,12"):
                            if(date>31){
                                month+1;
                                date=28-date;
                            }
                            break;

                        case ("4,6,9,11"):
                            if(date>30){
                                month+1;
                                date=28-date;
                            }
                            break;
                    }


                    var now=year+'-'+p(month)+"-"+p(date);
                    return "<input   type=\"text\" value='"+now+"' name='qixian' id='"+row.id+"q' style=\"border:0px;outline:medium;\">";
                },
            }],

    })


    $("#table2").bootstrapTable({
        url: '',
        striped:false,
        columns: [
            {
                field: 'house.id',
                title: 'id',
                visible : false,
            },{
                field: 'costitem.name',
                title: '缴费名称'
            },{
                field: 'costitem.money',
                title: '平米/金额'
            }
            ,{
                field: 'house.sale.name',
                title: '所属楼宇'
            },{
                field: 'house.danyuan.name',
                title: '所属单元'
            },{
                field: 'house.userdetails.name',
                title: '业主姓名'
            },{
                field: 'house.doornumber',
                title: '门牌号'
            },{
                field: 'house.measure',
                title: '房屋大小',

            },{
                field: 'zmoney',
                title: '应缴费用',
                formatter: function (value, row, index) {

                    var money = row.costitem.money * row.house.measure;
                    return "<input   type=\"text\" id='"+row.id+"j' value='"+money+"'   style=\"border:0px;outline:medium;\">";
                },
            },{
                field: 'bdate',
                title: '起收日期',
                formatter: function (value, row, index) {

                    function p(s) {
                        return s < 10 ? '0' + s: s;
                    }

                    var myDate = new Date();

                    var year=myDate.getFullYear();

                    var month=myDate.getMonth()+1;

                    var date=myDate.getDate();
                    var now=year+'-'+p(month)+"-"+p(date);
                    return now;
                },
            },{
                field: 'jdate',
                title: '缴费期限',
                formatter: function (value, row, index) {

                    function p(s) {
                        return s < 10 ? '0' + s: s;
                    }

                    var myDate = new Date();

                    var year=myDate.getFullYear();

                    var month=myDate.getMonth()+1;

                    var date=myDate.getDate()+7;

                    switch (month) {
                        case ("2"):

                            if(date>28){
                                month+1;
                                date=28-date;
                            }

                            break;
                        case ("1,3,5,7,8,10,12"):
                            if(date>31){
                                month+1;
                                date=28-date;
                            }
                            break;

                        case ("4,6,9,11"):
                            if(date>30){
                                month+1;
                                date=28-date;
                            }
                            break;
                    }
                    var now=year+'-'+p(month)+"-"+p(date);
                    return "<input   type=\"text\" value='"+now+"' name='qixian' id='"+row.id+"q' style=\"border:0px;outline:medium;\">";
                },
            }],

    })

    $("#dg").bootstrapTable({
        url:'${pageContext.request.contextPath}/../../order/list',
        columns: [
            {
                field: 'id',
                title: 'id',
                visible : false,
            },{
                field: 'orderid',
                title: '档案号',
            },{
                field: 'name',
                title: '档案名称',
            },{
                field: 'date',
                title: '建档日期',
            },{
                field: 'fname',
                title: '缴费名称',
            },{
                field: 'remark',
                title: '备注',
            },{
                field: 'srecords',
                title: 'list集合',
                visible : false,
            }
        ],
        contentType:"application/x-www-form-urlencoded",
        queryParamsType: '',
        //修改分页的默认对数
        //offset,limit
        height: 700,
        pageNumber: 1,
        pageSize: 10,
        pagination:true,
        queryParams: queryParams,
        sidePagination:'server',
        detailView:true,
        //当点击详细图标展开详细页面的时候触发。
        onExpandRow:function(index, row, $detail){
            console.log("123123")
            var aa=$detail.html("<table></table>").find('table');
            aa.bootstrapTable({
                url: '${pageContext.request.contextPath}/../../srecord/cc?orderid='+row.orderid,
                columns: [{
                    field: 'id',
                    title: 'id',
                    visible : false,
                },{
                    field: 'lasttime',
                    title: '上次读数'
                },{
                    field: 'sdate',
                    title: '上次日期'
                },{
                    field: 'jdate',
                    title: '缴费期限'
                },{
                    field: 'costitem.name',
                    title: '收费名称'
                },{
                    field:'num',
                    title:'用量',
                },{
                    field:'zmoney',
                    title:'金额',
                },{
                    field:'type',
                    title:'是否缴费',
                    formatter: function (value, row, index) {
                        if(row.type==1){
                            return "未交费";
                        }else if(row.type==2){
                            return "已缴费";
                        }
                    }

                }
                ],
            }) ;
        }

    })


    $("#save").hide();
    $("#save1").hide();

})

function queryParams(params) {

    var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
        'pageSize': params.pageSize,  //页面大小
        'pageNumber': params.pageNumber, //页码
        'date':$("#test6").val(),
        'number':$("#men").val(),
    };
    return temp;
};


function search() {
    var opt = $("#dg").bootstrapTable('getOptions');

    $.post('${pageContext.request.contextPath}/../../order/list', {
        'date':$("#test6").val(),
        'number':$("#men").val(),
        'pageSize': opt.pageSize,
        'pageNumber': 1
    }, function (data) {

        //重新给table绑定数据
        $("#dg").bootstrapTable("load", data);
    });
}




function diao(dd,qian){
    var ben = $(dd).val();
    var shang = $(dd).parent().prev().text();

    if(ben<shang){
        sweetAlert("不能比上一个月少！")
        return false;
    }

    var cha = ben-shang;

    $(dd).parent().next().children().val(cha);
    $(dd).parent().next().next().children().val(cha*qian);

}

function save(){
    var opt = $('#table1').bootstrapTable('getData');
    var name = $("#dname").val();
    var ddate = $("#ddate").val();
    var djname = $("#djname").val();
    var dremark = $("#dremark").val();
    var orderid = "123";
    $.post("${pageContext.request.contextPath}/../../order/add",{"orderid":orderid,"name":name,"date":ddate,"fname":djname,"remark":dremark},function(date){
        var e=date;
        for(var i = 0; i < opt.length; i++) {
            var ben = $("#"+opt[i].id).val();
            var num = $("#"+opt[i].id).parent().next().children().val();
            var yingjiao = $("#"+opt[i].id).parent().next().next().children().val();
            var ssj = $("#"+opt[i].id+"b").text();
            var qx = $("#"+opt[i].id+"q").val();
            console.log(ben);
            console.log(num);
            console.log(yingjiao);
            console.log(ssj);
            console.log(qx);

            $.post("${pageContext.request.contextPath}/../../srecord/add",{"id":opt[i].id,"lasttime":ben,"sdate":ssj,"bdate":ssj,"jdate":qx,"orderhao.id":e.id,"house.id":opt[i].house.id,"costitem.id":opt[i].costitem.id,"num":num,"zmoney":yingjiao,"type":1},function(){
                sweetAlert("保存成功！")
                $("#exampleModal").modal("hide");
            })
        }
    })
}

function save1(){
    var opt = $('#table2').bootstrapTable('getData');
    var name = $("#dname1").val();
    var ddate = $("#ddate1").val();
    var djname = $("#djname1").val();
    var dremark = $("#dremark1").val();
    var orderid = "123";
    $.post("${pageContext.request.contextPath}/../../order/add",{"orderid":orderid,"name":name,"date":ddate,"fname":djname,"remark":dremark},function(date) {
        var e = date;
        for (var i = 0; i < opt.length; i++) {
            // var ben = $("#"+opt[i].id).val();
            // var num = $("#"+opt[i].id).parent().next().children().val();
            // var yingjiao = $("#"+opt[i].id).parent().next().next().children().val();
            // var ssj = $("#"+opt[i].id+"b").text();
            // var qx = $("#"+opt[i].id+"q").val();
            var fei = $("#" + opt[i].id + "j").val();
            var qi = $("#" + opt[i].id + "j").parent().next().text();
            var jiao = $("#" + opt[i].id + "q").val();

            $.post("${pageContext.request.contextPath}/../../srecord/add2", {
                "id": opt[i].id,
                "sdate": qi,
                "bdate": qi,
                "jdate": jiao,
                "orderhao.id": e.id,
                "house.id": opt[i].house.id,
                "costitem.id": opt[i].costitem.id,
                "num": 0,
                "zmoney": fei,
                "type": 1
            }, function () {
                sweetAlert("保存成功！")
                $("#exampleModal1").modal("hide");
            })
        }
    })
}






























// 监控文本
// onkeyup="upperCase(this)"
// function upperCase(id){
//     console.log(id)
// }





